﻿using System;
using System.Windows;
using Microsoft.Win32;
using System.Reflection;

namespace ScreenLockerWPF
{
    class Locker
    {
        LockerWindow lw = new LockerWindow();

        System.Windows.Forms.Timer timeToPause = new System.Windows.Forms.Timer();
        System.Windows.Forms.Timer timeToWork  = new System.Windows.Forms.Timer();

        public Locker()
        {
            uint interval = Properties.Settings.Default.Interval;
            uint pause    = Properties.Settings.Default.PauseTime;

            timeToPause.Interval = Convert.ToInt32(interval * 60000);
            timeToWork.Interval  = Convert.ToInt32(pause * 60000);
            timeToPause.Tick    += new EventHandler(timeToPause_Tick);
            timeToWork.Tick     += new EventHandler(timeToWork_Tick);
        }

        private void timeToWork_Tick(object sender, EventArgs e)
        {
            Unlock();
            timeToWork.Stop();
            timeToPause.Start();
        }

        private void timeToPause_Tick(object sender, EventArgs e)
        {
            Lock();
            timeToPause.Stop();
            timeToWork.Start();
        }

        public void Start()
        {
            timeToPause.Start();
        }

        public void Stop()
        {
            timeToPause.Stop();
            timeToWork.Stop();
            Unlock();
        }

        public void Lock()
        {
            DisableTaskmanager();
            lw.Show();
        }

        public void Unlock()
        {
            EnableTaskmanager();
            lw.Hide();
        }

        public void SaveSettings(bool autorun, uint interval, uint pause)
        {
            if (autorun == true)
            {
                try
                {
                    EnableAutorun();
                    Properties.Settings.Default.Autorun = true;
                }
                catch { }
            }
            else if (autorun == false)
            {
                try
                {
                    DisableAutorun();
                    Properties.Settings.Default.Autorun = false;
                }
                catch { }
            }

            if (interval == 0)
                Properties.Settings.Default.Interval = interval + 1;
            else
                Properties.Settings.Default.Interval = interval;

            if (pause == 0)
                Properties.Settings.Default.PauseTime = pause + 1;
            else
                Properties.Settings.Default.PauseTime = pause;

            Properties.Settings.Default.Save();
        }

        private void EnableAutorun()
        {
            string runLocation      = @"Software\Microsoft\Windows\CurrentVersion\Run";
            string assemblyLocation = Assembly.GetExecutingAssembly().Location;
            RegistryKey key         = Registry.CurrentUser.CreateSubKey(runLocation);

            key.SetValue("ScreenLocker", assemblyLocation);
        }

        private void DisableAutorun()
        {
            string runLocation = @"Software\Microsoft\Windows\CurrentVersion\Run";
            RegistryKey key    = Registry.CurrentUser.CreateSubKey(runLocation);

            key.DeleteValue("ScreenLocker");
          
        }

        private void DisableTaskmanager()
        {
            RegistryKey regkey;
            string keyValueInt = "1";
            string subKey = @"Software\Microsoft\Windows\CurrentVersion\Policies\System";

            try
            {
                try
                { Registry.CurrentUser.DeleteSubKey(subKey); }
                catch { }

                regkey = Registry.CurrentUser.CreateSubKey(subKey);
                regkey.SetValue("DisableTaskMgr", keyValueInt);
                regkey.Close();
            }
            catch { }
        }

        private void EnableTaskmanager()
        {
            string subKey = "Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System";

            try
            { Registry.CurrentUser.DeleteSubKey(subKey); }
            catch { }
        }
    }


    partial class LockerWindow : Window
    {   
        public LockerWindow()
        {
            this.Width              = SystemParameters.VirtualScreenWidth;
            this.Height             = SystemParameters.VirtualScreenHeight;
            this.Top                = SystemParameters.VirtualScreenTop;
            this.Left               = SystemParameters.VirtualScreenLeft;
            this.WindowStyle        = System.Windows.WindowStyle.None;
            this.ResizeMode         = System.Windows.ResizeMode.NoResize;
            this.Background         = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Color.FromArgb(75, 0, 0, 0));
            this.AllowsTransparency = true;
            this.Topmost            = true;
            this.IsEnabled          = false;
            this.ShowInTaskbar      = false;         
            this.Closing           +=new System.ComponentModel.CancelEventHandler(Window_Closing);
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = true;
        }
    }
}
